package com.express.sys.controller;

import com.alibaba.fastjson.JSONObject;
import com.express.entity.Permission;
import com.express.mybatis.Result;
import com.express.sys.controller.base.BaseController;
import com.express.sys.service.PermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(value = "/permission")
public class PermissionController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(PermissionController.class);

	@Autowired
	PermissionService iPermissionService;

	@RequestMapping({"/listMenu"})
	public Result listMenu() {
		Result result = new Result();
		List<Permission> list = this.iPermissionService.listMenu();
		result.setData(list);
		return result;
	}

	@PostMapping({"/add"})
	public Result add(@RequestBody Permission permission) {
		Result result = new Result();
		if (this.iPermissionService.checkCode(permission)) {
			result.error("添加失败", "300");
			return result;
		}
		this.iPermissionService.add(permission);
		result.setData(permission);
		return result;
	}

	@PostMapping({"/update"})
	public Result update(@RequestBody Permission permission) {
		Result result = new Result();
		if (this.iPermissionService.checkCode(permission)) {
			result.error("更新失败", "300");
			return result;
		}
		this.iPermissionService.update(permission);
		return result;
	}

	@PostMapping({"/delete"})
	public Result delete(@RequestBody JSONObject json) {
		Result result = new Result();
		this.iPermissionService.delete(json.getInteger("id"));
		return result;
	}
}
